package gen.tools.query;

import gen.tools.Widgets;
import gen.tools.structures.S2;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;

public class QueryModUnit extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    public QueryModUnit() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request,response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		final Widgets wid = new Widgets();
		request.setCharacterEncoding( wid.UIcodeSet );
		response.setCharacterEncoding( wid.UIcodeSet );
		response.setContentType("application/json");
		
		final String subsysId = request.getParameter("subsysId");
		final String pageName = request.getParameter("pageName");
		
		ArrayList<String> mods = null;
		ArrayList<S2> modEncps = null;
		
		try {
			if ("modUnit.html".equalsIgnoreCase(pageName)) {
				modEncps = wid.getModUnitEncap(subsysId);
			} else {
				mods = wid.getModUnits(subsysId);
			}
			
		} catch( Exception e) {
			wid.errLog(e);
		}
		
		if ("modUnit.html".equalsIgnoreCase(pageName)) {
			response.getWriter().write( new Gson().toJson(modEncps) );	
		} else {
			response.getWriter().write( new Gson().toJson(mods) );	
		}
		
		
	}
}
